<?php

systeminc('session');
systeminc('ip');

if(DEBUG=="ON") if(file_exists('install/index.php')) system_error('Please remove the install-folder first.',0);

if(!function_exists('file_get_contents')) {
	function file_get_contents($filename) {
		$fd = fopen("$filename", "rb");
		$content = fread($fd, filesize($filename));
		fclose($fd);
		return $content;
	}
}

if(!function_exists('str_split')) {
	function str_split($str,$split_length=1) {

		$cnt = mb_strlen($str);

		for ($i=0;$i<$cnt;$i+=$split_length)
		$result[]= mb_substr($str,$i,$split_length);

		return $result;
	}
}

if(!function_exists('str_ireplace')) {
	function str_ireplace($search,$replace,$subject) {
		$search = preg_quote($search, "/");
		return preg_replace("/".$search."/i", $replace, $subject);
	}
}

function gettemplate($template,$endung="html", $calledfrom="root") {
	$templatefolder = "templates";
	if($calledfrom=='root') {
		return str_replace("\"", "\\\"",file_get_contents($templatefolder."/".$template.".".$endung));
	}
}

function str_break($str, $maxlen) {
	$nobr = 0;
	$str_br = '';
	$len = mb_strlen($str);
	for ($i = 0; $i<$len; $i++) {
		$char = mb_substr($str,$i,1);
		if (($char!=' ') && ($char!='-') && ($char!="\n")) $nobr++;
		else {
			$nobr = 0; 
			if($maxlen+$i>$len) {
				$str_br .= mb_substr($str, $i);
				break;
			}
		}
		if ($nobr>$maxlen) {
			$str_br .= '- '.$char;
			$nobr = 1;
		}
		else $str_br .= $char;
	}
	return $str_br;
}

function substri_count_array($haystack, $needle) {
	$return = 0;
	foreach($haystack as $value) {
		if(is_array($value)) {
			$return += substri_count_array($value, $needle);
		}
		else {
			$return += substr_count(strtolower($value), strtolower($needle));
		}
	}
	return $return;
}

function js_replace($string){
	$output=preg_replace("/(\\\)/si", '\\\\\1', $string);
	$output=str_replace(array("\r\n", "\n", "'", "<script>", "</script>", "<noscript>", "</noscript>"), array("\\n", "\\n", "\'", "\\x3Cscript\\x3E", "\\x3C/script\\x3E", "\\x3Cnoscript\\x3E", "\\x3C/noscript\\x3E"), $output);
	return $output;
}

function percent($sub, $total, $dec) {
	if ($sub) {
		$perc = $sub / $total * 100;
		$perc = round($perc, $dec);
		return $perc;
	}
	else return 0;
}

function checkenv($systemvar,$checkfor) {
	return stristr(ini_get($systemvar),$checkfor);
}

function createkey($length) {
	$key='';
	for($i=0;$i<$length;$i++) {
		switch(rand(1,3)) {
			case 1:
				$key.=chr(rand(48,57));
				break;
			case 2:
				$key.=chr(rand(65,90));
				break;
			case 3:
				$key.=chr(rand(97,122));
				break;
		}
	}
	return md5($key);
}

function mail_protect($mailaddress) {
	$protected_mail = "";
	$arr = unpack("C*", $mailaddress);
	foreach ($arr as $entry) {
		$protected_mail .= sprintf("%%%X", $entry);
	}
	return $protected_mail;
}

function validate_url($url) {
	return preg_match("/^[http:\/\/]+[a-z0-9_\.-]+[a-z0-9_-]+$/si",$url);
}
function validate_email($email){
	return preg_match("/^[a-z0-9_\.-]+@[a-z0-9_-]+\.[a-z0-9_\.-]+$/si",$email);
}
if(!function_exists('array_combine')){
	function array_combine($keyarray, $valuearray){
		$keys=array();
		$values=array();
		$result=array();
		foreach($keyarray AS $key){
			$keys[]=$key;
		}
		foreach($valuearray AS $value){
			$values[]=$value;
		}
		foreach($keys AS $access => $resultkey){
			$result[$resultkey]=$values[$access];
		}
		return $result;
	}
}

/* counts empty variables in an array */

function countempty($checkarray) {
	
	$ret = 0;
		
	foreach($checkarray as $value) {
		if(is_array($value)) $ret += countempty($value);
		elseif(trim($value) == "") $ret++;
	}
		
	return $ret;
}

/* checks, if given request-variables are empty */

function checkforempty($valuearray) {

	$check = Array();
	foreach($valuearray as $value) {
		$check[] = $_REQUEST[$value];
	}

	if(countempty($check) > 0) return false;
	return true;

}

function redirect($url, $info, $time=1) {
	if($url=="back" AND $info!='' AND isset($_SERVER['HTTP_REFERER'])) {
		$url = $_SERVER['HTTP_REFERER'];
		$info = '';
	} elseif($url=="back" AND $info!='') {
		$url = $info;
		$info = '';
	}
	echo'<meta http-equiv="refresh" content="'.$time.';URL='.$url.'" />
  <br /><p style="color:#000000">'.$info.'</p><br /><br />';
}



function cleartext($text, $bbcode=true, $calledfrom='root') {
	$text = htmlspecialchars($text);
	$text = strip_tags($text);
	$text = insertlinks($text,$calledfrom);
	$text = flags($text,$calledfrom);
	$text = replacement($text, $bbcode);
	$text = htmlnl($text);
	$text = nl2br($text);

	return $text;
}

function htmloutput($text) {
	$text = insertlinks($text);
	$text = replacement($text);
	$text = htmlnl($text);
	$text = nl2br($text);

	return $text;
}

function clearfromtags($text) {
	$text = getinput($text);
	$text = strip_tags($text);
	$text = htmlnl($text);
	$text = nl2br($text);

	return $text;
}

function getinput($text) {
	//$text = stripslashes($text);
	$text = htmlspecialchars($text);

	return $text;
}

function getforminput($text) {
	$text = str_replace(array('\r','\n'),array("\r","\n"),$text);
	$text = stripslashes($text);
	$text = htmlspecialchars($text);

	return $text;
}

function ago( $ptime )
{
	$etime = time() - $ptime;
	if( $etime < 1 )
	{
		return 'less than 1 second ago';
	}
	$a = array(	12 * 30 * 24 * 60 * 60	=>  'year',
				30 * 24 * 60 * 60		=>  'month',
				24 * 60 * 60			=>  'day',
				60 * 60				=>  'hour',
				60					=>  'minute',
				1					=>  'second'
	);
	foreach( $a as $secs => $str )
	{
		$d = $etime / $secs;

		if( $d >= 1 )
		{
			$r = round( $d );
			return $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
		}
	}
}

function substrhtml($str,$start,$len){

	$str_clean = substr(strip_tags($str),$start,$len);
	$pos = strrpos($str_clean, " ");
	if($pos === false) {
		$str_clean = substr(strip_tags($str),$start,$len);
	}else
		$str_clean = substr(strip_tags($str),$start,$pos);

	if(preg_match_all('/\<[^>]+>/is',$str,$matches,PREG_OFFSET_CAPTURE)){

		for($i=0;$i<count($matches[0]);$i++){

			if($matches[0][$i][1] < $len){

				$str_clean = substr($str_clean,0,$matches[0][$i][1]) . $matches[0][$i][0] . substr($str_clean,$matches[0][$i][1]);

			}else if(preg_match('/\<[^>]+>$/is',$matches[0][$i][0])){

				$str_clean = substr($str_clean,0,$matches[0][$i][1]) . $matches[0][$i][0] . substr($str_clean,$matches[0][$i][1]);

				break;

			}

		}

		return $str_clean;

	}else{
		$string = substr($str,$start,$len);
		$pos = strrpos($string, " ");
		if($pos === false) {
			return substr($str,$start,$len);
		}
		return substr($str,$start,$pos);

	}

}

// -- LOGIN -- //

$login_per_cookie = false;
if(isset($_COOKIE['ws_auth']) AND !isset($_SESSION['ws_auth'])) {
	$login_per_cookie = true;
	$_SESSION['ws_auth'] = $_COOKIE['ws_auth'];
}

systeminc('login');
systeminc('betfor5');


if($login_per_cookie) {
	$ll=mysql_fetch_array(safe_query("SELECT lastlogin FROM ".PREFIX."user WHERE userID='$userID'"));
	$_SESSION['ws_lastlogin'] = $ll['lastlogin'];
}

// -- SITE VARIABLE -- //

if(isset($_GET['p'])) $p = $_GET['p'];
else $p = '';
if(!isset($_SERVER['HTTP_REFERER'])) {
	$_SERVER['HTTP_REFERER'] = "";
}

?>